#include "sdk/include/notebookeditor.h"
#include "sdk/include/editormgr.h"
#include "sdk/include/editor.h"

namespace sdk
{
    BEGIN_EVENT_TABLE( NotebookEditor, wxAuiNotebook )
        EVT_AUINOTEBOOK_PAGE_CLOSE( wxID_ANY, NotebookEditor::OnPageClose )
        EVT_SCI_SAVEPOINTREACHED( wxID_ANY, NotebookEditor::OnSavePointReached )
        EVT_SCI_SAVEPOINTLEFT( wxID_ANY, NotebookEditor::OnSavePointLeft )
    END_EVENT_TABLE()

    NotebookEditor::NotebookEditor( wxWindow *parent, wxWindowID id )
        : wxAuiNotebook( parent, id )
    {

    }

    NotebookEditor::~NotebookEditor()
    {

    }

    void NotebookEditor::OnPageClose( wxAuiNotebookEvent &evt )
    {
        const int index = evt.GetSelection();
        EditorManager::Get()->CloseEditor( dynamic_cast<sdk::Editor *>( GetPage( index ) ) );
        evt.Veto();
    }

    void NotebookEditor::OnSavePointReached( wxScintillaEvent &evt )
    {
        const int index = GetPageIndex( dynamic_cast<sdk::Editor *>( evt.GetEventObject() ) );
        const wxString title = GetPageText( index );
        SetPageText( index, title.AfterFirst( wxT( '*' ) ) );

        evt.Skip();
    }

    void NotebookEditor::OnSavePointLeft( wxScintillaEvent &evt )
    {
        const int index = GetPageIndex( dynamic_cast<sdk::Editor *>( evt.GetEventObject() ) );
        const wxString title = GetPageText( index );
        SetPageText( index, wxT( '*' ) + title );

        evt.Skip();
    }
}
